package cn.edu.javaweb.userServlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.javaweb.utils.DBUtil;

@WebServlet("/EnrollServlet")
public class EnrollServlet extends HttpServlet {
	DBUtil db;
	private static final long serialVersionUID = 1L;
       
    
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码方式
	    request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");   //不设置会乱码
				
		String recruit_id=request.getParameter("id");
		String person=request.getParameter("person");
		String progress=request.getParameter("progress");
		System.out.println(recruit_id);
		
		//从Login.html获取id
		HttpSession session = request.getSession(true);     // 如果不存在 session 会话，则创建一个 session 对象
		String userid = session.getAttribute("id").toString();
		
		//JDBC连接数据库
		db = new DBUtil();  //调用构造函数，数据库的搭建
		String SQL = "insert into enroll (userid,recruit_id) values ("+userid+","+recruit_id+");";
		System.out.println(SQL);
		int result=db.update(SQL);  //调用（工具类）update方法，result来传递结果
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		if(result>0){
			out.println("<script language=javascript>alert('报名成功！');window.location='PersonalRecruitSelectServlet';</script>");			
		}else{
			out.println("<script language=javascript>alert('您已报名，请勿重复报名');window.location='RecruitServlet';</script>");
		}	
		
		String SQL2 = "update notice set person="+person+"+1,progress="+progress+"+2 where id='"+recruit_id+"'";
		System.out.println(SQL2);
		int result2=db.update(SQL2);  //调用（工具类）update方法，result来传递结果
		response.setContentType("text/html;charset=utf-8");
		System.out.println(result2);		
	}
}
